package com.weirdor.wxchat.facade.user;

import com.weirdor.wxchat.facade.user.vo.Wxuser;
import com.weirdor.wxchat.facade.vo.menu.WeixinButton;
import com.weirdor.wxchat.support.exception.OtherAPIException;
import com.weirdor.wxchat.support.exception.WexinReqException;
import com.weirdor.wxchat.support.httpClient.HttpsClient;
import com.weirdor.wxchat.support.utils.JsonUtils;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by weirdor on 2017/11/28.
 */
public class UserAPI {

    public final static String user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info";

    public final static String user_list_url = "https://api.weixin.qq.com/cgi-bin/user/get";

    public final static String user_mark_url = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark";




    /**
     * 根据user_openid 获取关注用户的基本信息
     *
     * @param user_openid
     * @return
     * @throws WexinReqException
     */
    public static Wxuser  getWxuser(String accesstoken,String user_openid) throws WexinReqException, OtherAPIException {
        if (accesstoken != null) {
            Map<String,String> map=new HashMap<>();
            map.put("access_token",accesstoken);
            map.put("openid",user_openid);
            map.put("lang","zh_CN");
            String response= HttpsClient.doGet(user_info_url,map);
            JSONObject result = JSONObject.fromObject(response);
            System.out.println(result);
            // 正常返回
            Wxuser wxuser = null;
            Object error = result.get("errcode");
            wxuser = (Wxuser) JSONObject.toBean(result, Wxuser.class);
            return wxuser;
        }
        return null;
    }
    /**
     * 获取所有关注用户信息信息
     *
     * @return
     * @throws WexinReqException
     */
    public static List<Wxuser> getAllWxuser(String accesstoken, String next_openid) throws WexinReqException, OtherAPIException {
        if (accesstoken != null) {
            Map<String,String> map=new HashMap<>();
            map.put("access_token",accesstoken);
            map.put("next_openid",next_openid);
            String response= HttpsClient.doGet(user_list_url,map);
            JSONObject result = JSONObject.fromObject(response);
            System.out.println(result);
            Object error = result.get("errcode");
            List<Wxuser> lstUser = null;
            Wxuser mxuser = null;
            int total = result.getInt("total");
            int count = result.getInt("count");
            String strNextOpenId = result.getString("next_openid");
            JSONObject data = result.getJSONObject("data");
            lstUser = new ArrayList<Wxuser>(total);
            if (count > 0) {
                JSONArray lstOpenid = data.getJSONArray("openid");
                int iSize = lstOpenid.size();
                for (int i = 0; i < iSize; i++) {
                    String openId = lstOpenid.getString(i);
                    mxuser = getWxuser(accesstoken, openId);
                    lstUser.add(mxuser);
                }
                if (strNextOpenId != null) {
                    lstUser.addAll(getAllWxuser(accesstoken, strNextOpenId));
                }
            }
            return lstUser;
        }
        return null;
    }

    /**
     * 设置用户备注名
     *
     * */
    public static String createMenu(String accessToken,String remark,String openid) throws WexinReqException, OtherAPIException {
        Map<String,Object> map=new HashMap<>();
        map.put("remark",remark);
        map.put("openid",openid);
        String requestUrl = user_mark_url+"?access_token="+accessToken;
        String response= HttpsClient.doPost(requestUrl, JsonUtils.to(map));
        JSONObject jsonObject=JSONObject.fromObject(response);
        String msg = jsonObject.getString("errmsg");
        return msg;
    }


    public static void main(String[] args) throws OtherAPIException, WexinReqException {
        System.out.println( getAllWxuser("DHmd3qFzxjTALsqmTVTNOU4qmbUXqxiBlPEaGlts5SKaS1Nt0oHWQt8y7mb7JqLCtwxMAE_Q883YlkR8S12JiGYobNyFE-rdVTQSdXjEo0YKSCiAIABJT",""));
    }

}
